#include <cstdio>
#include <cstdlib>

typedef struct Node {
    int data;
    struct Node *pre;
    struct Node *next;
} Node;

Node *initList() {
    Node *list = (Node *)malloc(sizeof(Node));
    list->data = 0;
    list->pre = nullptr;
    list->next = nullptr;
    return list;
}

void headInsert(Node *list, int val) {
    if(list == nullptr) return ;
    Node *node = (Node *)malloc(sizeof(Node));
    node->data = val;
    if(list->data == 0) {
        node->next = list->next;
        node->pre = list;
        list->next = node;
        list->data++;
    } else {
        node->pre = list;
        node->next = list->next;
        list->next->pre = node;
        list->next = node;
        list->data++;
    }
    return ;
}
void tailInsert(Node *list, int val) {
    if(list == nullptr) return ;
    Node *node = (Node *)malloc(sizeof(Node));
    node->data = val;
    Node *tmp = list;
    while(tmp->next) {
        tmp = tmp->next;
    }
    node->next = tmp->next;
    node->pre = tmp;
    tmp->next = node;
    list->data++;
    return ;
}
bool deleteElement(Node *list, int val) {
    if(list == nullptr) return false;
    Node *node = list->next;
    while(node) {
        if(node->data == val) {
            node->pre->next = node->next;
            node->next->pre = node->pre;
            free(node);
            list->data--;
            return true;
        }
        node = node->next;
    }
    return false;
}
void printList(Node *list) {
    Node *node = list->next;
    while(node) {
        printf("%3d ->", node->data);
        node = node->next;
    }
    printf(" NULL\n\n");
}
int main() {
    Node *list = initList();
    for(int i = 1; i<= 5; i++) {
        headInsert(list, i);
    }
    printList(list);
    for(int i = 10; i<= 15; i++) {
        tailInsert(list, i);
    }
    printList(list);
    return 0;
}